//
//  RDMineViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/5/28.
//

import UIKit
import RxSwift
import TangramKit


class RDMineViewController: SimpleViewController<RDMineViewModel> {
    
    override func loadView() {
        
        let scrollView = UIScrollView()
        scrollView.backgroundColor = .white
        self.view = scrollView
        
        let contentLayout = TGLinearLayout(.vert)
        contentLayout.tg_padding = UIEdgeInsets(top: 10,left: 10,bottom: 10,right: 10) //设置布局内的子视图离自己的边距.
        contentLayout.tg_width.equal(.fill);  //设置视图自身的宽度填充父视图的剩余宽度。
        contentLayout.tg_height.equal(.wrap).min(scrollView.tg_height, increment: 10)
        scrollView.addSubview(contentLayout)
        
        
        contentLayout.addSubview(UIButton().then { [weak self] view in
            view.tg_top.equal(5)
            view.tg_width.equal(.fill)
            view.tg_height.equal(40)
            guard let this = self else {
                return
            }
            view.setTitleAndColor("退出登陆")
            view.rx.tap.subscribe(onNext: {
                // 进行退出
                UserDefaults.standard.removeObject(forKey: "token")
                UserDefaults.standard.removeObject(forKey: "crmId")
                this.viewModel?.postMessage(RDNotification.quit)
            }).disposed(by: this.rx.disposeBag)
        })
        
        contentLayout.addSubview(UIButton().then { [weak self] view in
            view.tg_top.equal(5)
            view.tg_width.equal(.fill)
            view.tg_height.equal(40)
            guard let this = self else {
                return
            }
            view.setTitleAndColor("刷新组织列表")
            view.rx.tap.subscribe(onNext: {
                this.viewModel?.postMessage(RDNotification.getOrgList)
            }).disposed(by: this.rx.disposeBag)
            
        })
        
        contentLayout.addSubview(UIButton().then { [weak self] view in
            view.tg_top.equal(5)
            view.tg_width.equal(.fill)
            view.tg_height.equal(40)
            guard let this = self else {
                return
            }
            view.setTitleAndColor("创建数据库")
            view.rx.tap.subscribe(onNext: {
                // 进行退出
                
            }).disposed(by: this.rx.disposeBag)
        })
        
        contentLayout.addSubview(UIButton().then { [weak self] view in
            view.tg_top.equal(5)
            view.tg_width.equal(.fill)
            view.tg_height.equal(40)
            guard let this = self else {
                return
            }
            view.setTitleAndColor("testInt")
            view.rx.tap.subscribe(onNext: {
                // 进行退出
                print(" 打印 \(Int.max) \(Int32.max) \(Int64.max)")
            }).disposed(by: this.rx.disposeBag)
        })
        
        contentLayout.addSubview(UIButton().then { [weak self] view in
            view.tg_top.equal(5)
            view.tg_width.equal(.fill)
            view.tg_height.equal(40)
            guard let this = self else {
                return
            }
            view.setTitleAndColor("空页面的开发")
            view.rx.tap.subscribe(onNext: {
                // 进行退出
                // 说明没有组织 需要设置空页面
                EmptyDataView().setCallback({ view in
                    view.removeFromSuperview()
                }).addTo(this.view.superview!) { view in
                    view.snp.makeConstraints { make in
                        make.edges.equalToSuperview()
                    }
                }
            }).disposed(by: this.rx.disposeBag)
        })
        
        contentLayout.addSubview(TitlePullDownButton().then { [weak self] view in
            view.tg_top.equal(5)
            view.tg_width.equal(.fill)
            view.tg_height.equal(40)
        })
        
        var layoutView = TGLinearLayout(.horz)
        layoutView.tg_width.equal(.fill)
        layoutView.tg_height.equal(.wrap)
        
        let redView = RedUnreadView().then {view in
            view.tg_top.equal(5)
            view.tg_width.equal(.wrap)
            view.tg_height.equal(.wrap)
        }
        layoutView.addSubview(redView)
        
        let addRed = UIButton().then({ [weak self]view in
            view.setTitleAndColor("Add")
            view.tg_top.equal(5)
            view.tg_width.equal(.wrap)
            view.tg_height.equal(.wrap)
            view.rx.tap.subscribe { element in
                redView.num = redView.num + 1
            }.disposed(by: self!.rx.disposeBag)
        })
        layoutView.addSubview(addRed)
        contentLayout.addSubview(layoutView)
        
    }
}

class RDMineViewModel : SimpleViewModel{
    
}
